//
//  PAFeedbackView.m
//  PAMobileWallet
//
//  Created by shen pu on 13-7-18.
//  Copyright (c) 2013年 平安付. All rights reserved.
//

#import "PAFeedbackView.h"
#import "PABaseViewController.h"

@implementation PAFeedbackView
@synthesize feedbackTV;
@synthesize feedbackTF;
@synthesize bgView;
@synthesize feedbackScrollerView;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        CGFloat x, y, w, h;
        
        /*
        x = 0; y = 44; w = 320; h = (iPhone5?548:460) - 44;
        self.bgView = [[UIView alloc] initWithFrame:CGRectMake(x, y, w, h)];
        self.bgView.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.bgView];
        */
        
        x = 0; y = 44; w = 320; h = iPhone5?548:460 - 44;
        feedbackScrollerView = [[UIScrollView alloc] initWithFrame:CGRectMake(x, y, w, h)];
        feedbackScrollerView.backgroundColor = [UIColor whiteColor];
        feedbackScrollerView.delegate = self;
        feedbackScrollerView.pagingEnabled = NO;
        feedbackScrollerView.showsVerticalScrollIndicator = NO;
        feedbackScrollerView.showsHorizontalScrollIndicator = NO;
        feedbackScrollerView.backgroundColor = [UIColor clearColor];
        CGSize newSize = CGSizeMake(320,  iPhone5?548:460 + 140);
        [feedbackScrollerView setContentSize:newSize];
        [self addSubview:feedbackScrollerView];
        
        x = 15; y = 5; w = 290; h = 60;
        UILabel * feedbackTitleLable = [[UILabel alloc] initWithFrame:CGRectMake(x, y, w, h)];
        feedbackTitleLable.backgroundColor = [UIColor clearColor];
        feedbackTitleLable.numberOfLines = 2;
        feedbackTitleLable.text = @"欢迎提出宝贵意见和建议，您留下的每个字都将用来改善我们的软件。";
        feedbackTitleLable.font = [UIFont systemFontOfSize:17.0f];
        feedbackTitleLable.textColor = [UIColor darkTextColor];
        [feedbackScrollerView addSubview:feedbackTitleLable];
        
        x = 15; y = 5 + 60 + 5; w = 290; h = 160;
        UIImageView * feedbackTVImage = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, w, h)];
        feedbackTVImage.backgroundColor = [UIColor clearColor];
        feedbackTVImage.image = [UIImage imageNamed:@"set_feedback.png"];
        [feedbackScrollerView addSubview:feedbackTVImage];

        self.feedbackTV = [[UITextView alloc] initWithFrame:CGRectMake(x, y, w, h)];
        self.feedbackTV.font = [UIFont systemFontOfSize:14.0f];
        self.feedbackTV.delegate = self;
        self.feedbackTV.keyboardType = UIKeyboardTypeDefault; //键盘类型
        self.feedbackTV.backgroundColor = [UIColor clearColor];
        self.feedbackTV.scrollEnabled = YES;//是否可以拖动
        self.feedbackTV.autoresizingMask = UIViewAutoresizingFlexibleHeight;//自适应高度
        [feedbackScrollerView addSubview:self.feedbackTV];
        
        x = 30; y = 5 + 60 + 5 + 160; w = 290; h = 30;
        UILabel * contectWay = [[UILabel alloc] initWithFrame:CGRectMake(x, y, w, h)];
        contectWay.backgroundColor = [UIColor clearColor];
        contectWay.numberOfLines = 2;
        contectWay.text = @"联系方式";
        contectWay.font = [UIFont boldSystemFontOfSize:17.0f];
        contectWay.textColor = [UIColor grayColor];
        [feedbackScrollerView addSubview:contectWay];
        
        x = 15; y = 5 + 60 + 5 + 160 + 30 + 5; w = 290; h = 40;
        self.feedbackTF = [[UITextField alloc] initWithFrame:CGRectMake(x, y, w, h)];
        [self.feedbackTF setBorderStyle:UITextBorderStyleNone]; //外框类型
        self.feedbackTF.keyboardType = UIKeyboardTypeDefault;
        self.feedbackTF.returnKeyType = UIReturnKeyDone;
        self.feedbackTF.placeholder = @"请输入联系方式"; //默认显示的字
        self.feedbackTF.secureTextEntry = NO; //密码
        self.feedbackTF.delegate = self;
        self.feedbackTF.contentHorizontalAlignment = NSTextAlignmentCenter;
        self.feedbackTF.contentVerticalAlignment = NSTextAlignmentLeft;
        self.feedbackTF.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
        self.feedbackTF.background = [UIImage imageNamed:@"textbox2.png"];
        [feedbackScrollerView addSubview:self.feedbackTF];
        
        x = 15; y = 5 + 60 + 5 + 160 + 30 + 40 + 20; w = 290; h = 49;
        //确认按钮
        UIButton * loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
        loginButton.frame = CGRectMake(x, y, w, h);
        loginButton.backgroundColor = [UIColor clearColor];
        [loginButton setTitle:@"确认" forState:UIControlStateNormal];
        [loginButton setBackgroundImage:[UIImage imageNamed:@"button_A1.png"] forState:UIControlStateNormal];
        [loginButton setBackgroundImage:[UIImage imageNamed:@"button_A2.png"] forState:UIControlStateHighlighted];
        loginButton.titleLabel.textColor = ABLE_COLOR;
        loginButton.titleLabel.font = [UIFont systemFontOfSize:18.0];
        loginButton.tag = 0;
        [loginButton addTarget:self action:@selector(gobackAction:) forControlEvents:UIControlEventTouchUpInside];
        [feedbackScrollerView addSubview:loginButton];
    }
    return self;
}

- (void)gobackAction:(id)sender
{
    [self.feedbackTF resignFirstResponder];
    [self.feedbackTV resignFirstResponder];
    
    CGFloat x, y, w, h;
    x = 0; y = 44; w = 320; h = (iPhone5?548:460) - 44;
    self.feedbackScrollerView.frame = CGRectMake(x, y, w, h);

    if (self.parentController)
    {
        [self.parentController testNext:sender];
    }
}

#pragma mark - UITextView Delegate Methods

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    /*
    CGFloat x, y, w, h;
    
    x = 0; y = 44 - 60; w = 320; h = (iPhone5?548:460) - 44;
    self.feedbackScrollerView.frame = CGRectMake(x, y, w, h);
    */
    
    [self.feedbackScrollerView setContentOffset:CGPointMake(0, 60)];
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    /*
    if ([text isEqualToString:@"\n"])
    {
        [textView resignFirstResponder];
        
        CGFloat x, y, w, h;
        x = 0; y = 44; w = 320; h = (iPhone5?548:460) - 44;
        self.feedbackScrollerView.frame = CGRectMake(x, y, w, h);
        
        return NO;
    }
    */
    
    return YES;
}

#pragma mark - UITextFieldDelegate  Methods

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    /*
    CGFloat x, y, w, h;
    
    x = 0; y = iPhone5?(88-120):(-120); w = 320; h = (iPhone5?548:460) - 44;
    self.feedbackScrollerView.frame = CGRectMake(x, y, w, h);
     */
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    
    /*
    CGFloat x, y, w, h;
    
    x = 0; y = 44; w = 320; h = (iPhone5?548:460) - 44;
    self.feedbackScrollerView.frame = CGRectMake(x, y, w, h);
     */
    
    [self.feedbackScrollerView setContentOffset:CGPointMake(0, 0)];

    return YES;
}

@end
